package org.pettyfox.tool.pm;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.util.Date;

/**
 * @author pettyfox
 */
public class Controller {
    private final String DEFAULT_KEY_PATH = "/home/key.bin";
    private PasswordMe passwordMe = new PasswordMe();
    private String keyPath ;
    private String passwordPath ;
    @FXML
    private TextField editKeyPath;
    @FXML
    private TextField editPasswordPath;
    @FXML
    private TextField textPwd;
    @FXML
    private TextField textBuildPath;
    @FXML
    private TextField editKeyPath1;
    @FXML
    private TextField textPwdName;
    @FXML
    private TextArea textPassword;
    @FXML
    private Button btnBuildKey;
    @FXML
    public void initialize() {
        if(new File(DEFAULT_KEY_PATH).exists()){
            this.keyPath =DEFAULT_KEY_PATH;
        }
        btnBuildKey.setOnMouseClicked((event -> {
            String path = passwordMe.buildKey();
            System.out.println(path);
            this.keyPath = path;
            this.editKeyPath1.setText(this.keyPath);
        }));

    }
    public void hint(String msg){
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("info:");
        alert.setContentText(msg);
        alert.setContentText(msg);

        alert.showAndWait();
    }
    public void clickDecode(ActionEvent event){
        if(StrUtil.isBlank(this.keyPath)){
            hint("please upload key");
            return;
        }
        if(StrUtil.isBlank(passwordPath)){
            hint("please upload password");
            return;
        }
        byte[] key = FileUtil.readBytes(keyPath);
        byte[] password = FileUtil.readBytes(passwordPath);
        try{
            String pwd = passwordMe.decode(key,password);
            if(StrUtil.isBlank(pwd)){
                textPassword.setText("decode error");
            }else{
                textPassword.setText(pwd);
            }
        }catch (Exception ee){
            ee.printStackTrace();
            textPassword.setText("decode error");
        }
    }
    public void buildPassword(ActionEvent event){
        if(StrUtil.isBlank(this.keyPath)){
            hint("please upload key");
            return;
        }
        byte[] key = FileUtil.readBytes(keyPath);
        String name = DateUtil.formatDateTime(new Date()).replace(" ","-")+".bin";
        String path = passwordMe.encode(key,this.textPwd.getText().getBytes(),name);
        textPwdName.setText(name);
        textBuildPath.setText(path);
    }
    public void clickUploadKey(ActionEvent actionEvent){
        Stage mainStage = null;
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Upload Key");
        File file = fileChooser.showOpenDialog(mainStage);
        editKeyPath.setText(file.getAbsolutePath());
        editKeyPath1.setText(file.getAbsolutePath());
        this.keyPath = file.getAbsolutePath();
        this.textPassword.clear();
    }
    public void clickUploadPassword(ActionEvent actionEvent){
        Stage mainStage = null;
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Upload Password");
        File file = fileChooser.showOpenDialog(mainStage);
        editPasswordPath.setText(file.getAbsolutePath());
        this.passwordPath = file.getAbsolutePath();
        this.textPassword.clear();
    }

}
